package com.example.sharesinfo.helper;

import android.content.ContentValues;

import com.example.sharesinfo.db.ChatHistory;
import com.example.sharesinfo.db.Questions;
import com.example.sharesinfo.db.WordHistory;
import com.example.sharesinfo.utils.LogUtils;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.util.List;

/**
 * FileName: LitePalHelper
 * Founder:
 * Profile: 本地数据库帮助类
 */
public class LitePalHelper {
    private static volatile LitePalHelper mInstnce = null;

    private LitePalHelper() {

    }

    public static LitePalHelper getInstance() {
        if (mInstnce == null) {
            synchronized (LitePalHelper.class) {
                if (mInstnce == null) {
                    mInstnce = new LitePalHelper();
                }
            }
        }
        return mInstnce;
    }

    /**
     * 保存基类
     *
     * @param support
     */
    private void baseSave(LitePalSupport support) {
        support.save();
    }

    /**
     * 查询的基类
     *
     * @param cls
     * @return
     */
    private List<? extends LitePalSupport> baseQuery(Class cls) {
        return LitePal.findAll(cls, true);
    }

    /**
     * 保存与客服聊天记录
     *
     * @param msg
     * @param type
     */
    public void saveHistory(String msg, int type, List<Questions> questions) {
        ChatHistory chat = new ChatHistory();
        chat.setType(type);
        chat.setText(msg);
        chat.setList(questions);
        baseSave(chat);
        if (questions != null) {
            for (int i = 0; i < questions.size(); i++) {
                Questions question = questions.get(i);
                saveQuestions(question.getQuestionId(), question.getQuestion(), chat.getId());
            }
        }
    }

    /**
     * 保存智能推荐推荐
     *
     * @param id
     * @param question
     */
    public void saveQuestions(String id, String question, int chatId) {
        LogUtils.i("saveQuestions");
        Questions questions = new Questions();
        questions.setQuestionId(id);
        questions.setQuestion(question);
        questions.setChathistory_id(chatId);
        LogUtils.i("saveQuestions" + question);
        baseSave(questions);
    }

    /**
     * 用户搜索词条的历史记录
     *
     * @param word
     */
    public void saveWord(String word) {
        LogUtils.i("saveWord");
        WordHistory wordHistory = new WordHistory();
        wordHistory.setContent(word);
        baseSave(wordHistory);
    }

    /**
     * 用户搜索词条的历史记录
     *
     * @return
     */
    public List<WordHistory> queryWordHistory() {
        return (List<WordHistory>) baseQuery(WordHistory.class);
    }

    /**
     * 查询客服聊天记录
     *
     * @return
     */
    public List<ChatHistory> queryHistory() {
        return (List<ChatHistory>) baseQuery(ChatHistory.class);
    }

    /**
     * 查询推荐问题
     *
     * @return
     */
    public List<Questions> queryQuestions() {
        return (List<Questions>) baseQuery(Questions.class);
    }

    public Questions findLast() {
        return LitePal.findLast(Questions.class);
    }

    /**
     * 删除客服聊天记录
     *
     * @return
     */
    public void delHistory() {
        LitePal.deleteAll(ChatHistory.class);
        LitePal.deleteAll(Questions.class);

    }

    /**
     * 删除用户搜索词条的历史记录
     *
     * @return
     */
    public void delWordHistory() {
        LitePal.deleteAll(WordHistory.class);
    }

    /**
     * 更新问题已读状态
     *
     * @param Id
     * @param isClicked
     */
    public void updateQuestion(int Id, int isClicked) {
        ContentValues values = new ContentValues();
        values.put("isClicked", isClicked);
        LitePal.update(Questions.class, values, Id);
    }

}
